package tower.ai.base.chat.service;

import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OllamaEmbedService {

    @Resource(name = "qdrantVectorStore")
    private VectorStore vectorStore;

    public void save(String content){
        if(StringUtils.isNotBlank(content)){
            vectorStore.add(List.of(new Document(content)));
        }
    }

    public void saveList(List<String> content){
        if(content != null && ! content.isEmpty()) {
            List<Document> documents = new ArrayList<>(content.size());
            content.stream().filter(n -> StringUtils.isNotBlank(n)).forEachOrdered(n -> documents.add(new Document(n)));
            vectorStore.add(documents);
        }
    }

    public List<Document> search(String query){
        if(StringUtils.isNotBlank(query)){
            return vectorStore.similaritySearch(SearchRequest.query(query).withSimilarityThresholdAll());
        }
        return new java.util.ArrayList<Document>(0);
    }

    public List<Document> test(){
        List<String> documents = List.of(
                "Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!",
                "The World is Big and Salvation Lurks Around the Corner",
                "You walk forward facing the past and you turn back toward the future.");
        saveList(documents);
        List<Document> result = this.search("How about Spring?");
        return result;
    }


}
